<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Review
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Review model
 *
 * @method Mage_Review_Model_Resource_Review _getResource()
 * @method Mage_Review_Model_Resource_Review getResource()
 * @method string getCreatedAt()
 * @method Mage_Review_Model_Review setCreatedAt(string $value)
 * @method Mage_Review_Model_Review setEntityId(int $value)
 * @method int getEntityPkValue()
 * @method Mage_Review_Model_Review setEntityPkValue(int $value)
 * @method int getStatusId()
 * @method Mage_Review_Model_Review setStatusId(int $value)
 *
 * @category    Mage
 * @package     Mage_Review
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Shangku_ReviewsWithImg_Model_Review extends Mage_Review_Model_Review
{   
    protected function _afterSave()
    {
    	if($_FILES){
    		$reviewId = $this->getId();
    		$destinationFolder = Mage::getBaseDir('media') . DS . 'review';
    		foreach(array('image1', 'image2') as $image){
    			if (isset($_FILES[$image]['error']) && $_FILES[$image]['error'] == 0){
		    		$uploader = new Varien_File_Uploader($image);
		    		$uploader->setAllowCreateFolders(true);
		    		$uploader->setAllowRenameFiles(true);
		    		$uploader->setAllowedExtensions(array('jpg'));
		    		$uploader->setFilesDispersion(true);
		    		$uploader->addValidateCallback('review_image_validate_callback', $this, 'validateUploadFile');
		    		$result = $uploader->save($destinationFolder);
		    		if($result){
		    			Mage::getModel('reviewswithimg/review_image')
			    			->setData(array('review_id' => $reviewId, 'img_name' => $result['file']))
			    			->save();
		    		}
    			}
    		}
    	}
    	return parent::_afterSave();
    }
    public function validateUploadFile($filePath)
    {
    	if (filesize($filePath) > (2*1024*1024)) {
    		Mage::throwException($this->__('Individual photo size cannot exceed 2MB.'));
    	}
    	return true;
    }
}
